package com.zoe.wan.android.example.activity.knowledge

import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ActivityKnowledgeDetialBinding
import com.zoe.wan.android.example.databinding.ItemTabBinding
import com.zoe.wan.android.example.repositry.data.knowledge.DetailIntentData
import com.zoe.wan.base.BaseActivity
import com.zoe.wan.base.adapter.Pager2Adapter

class KnowledgeDetialActivity :
    BaseActivity<ActivityKnowledgeDetialBinding, KnowledgeDetialViewModel>() {


    companion object {

        const val INTENT_TAV_DATA = "INTENT_TAV_DATA"

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_knowledge_detial
    }

    override fun getViewModelId(): Int {
        return BR.konwledgeVm
    }

    override fun initViewData() {
        val model = intent.getSerializableExtra(INTENT_TAV_DATA) as DetailIntentData
        val pages = mutableListOf<Fragment>()
        var tabTitles = mutableListOf<String>()
        model.tabList?.forEach {
            it.name?.let { it1 -> tabTitles.add(it1) }
            it.id?.let {it1 ->  pages.add(FragDetailList(it1)) }
        }

        val adapter = Pager2Adapter(this@KnowledgeDetialActivity)
        adapter.setData(pages)

        binding?.detailViewPager2?.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        binding?.detailViewPager2?.adapter = adapter

//
        if (binding?.detialTabLayout == null || binding?.detailViewPager2 == null) {
            return
        }
//        // tablayout与viewpage2绑定
        val tabLayout = TabLayoutMediator(
            binding?.detialTabLayout!!,
            binding?.detailViewPager2!!,
            true,
            true
        ) { tab, index ->
            val tabItemBinding = DataBindingUtil.inflate<ItemTabBinding>(
                layoutInflater,
                R.layout.item_tab,
                null,
                false
            )

            tabItemBinding.tabItemTitle.text = tabTitles[index]
            tab.customView = tabItemBinding.root

        }.attach()

    }


}